package com.sysmanage.permissions.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sysmanage.common.tools.page.Pagination;
import com.sysmanage.framework.dao.hspring.DAOInterface;
import com.sysmanage.framework.service.BaseService;
import com.sysmanage.permissions.ConstCode;
import com.sysmanage.permissions.dao.ResourcesDao;
import com.sysmanage.permissions.dao.SystemDao;
import com.sysmanage.permissions.model.Resources;
import com.sysmanage.permissions.model.Systems;
import com.sysmanage.permissions.service.IResourcesService;
import com.sysmanage.permissions.service.ISystemService;

@Service
@Transactional
public class SystemService extends BaseService<Systems> implements ISystemService {

	@Autowired
	private SystemDao systemDao;
	
	@Resource(name="resourcesDao")
	private ResourcesDao resourcesDao;	
	
	@Autowired
	private IResourcesService resourcesService;
	
	public Pagination getPageList(Systems sys,int page, int rows,String sort,String order) {
		return systemDao.getPageList(sys,page, rows,sort,order);
	}
	public List<Systems> getAll(){
		return systemDao.getAll();
	}

	@Override
	protected DAOInterface<Systems> getDAO() {
		return systemDao;
	}
	public boolean saveSystem(Systems system,boolean save)
	{
		//系统保存服务
		//系统保存时，在资源表创建一条同样的记录
		//更新时，更新资源表的对应记录,如果不存在，则创建
		boolean result = false;
		Integer systemId = 0;
		boolean ret = false;
		
		try {
			if(save)
			{
				ret =systemDao.save(system);
			}
			else
			{			
				//防止死循环
				//如果父节点等于自身，则设置为null
				if(system.getId().equals(system.getParentId()))
				{
					system.setParentId(null);
				}				
				ret = systemDao.alter(system);
			}
			if(ret)
			{
				systemId = system.getId();
				String hsql = "from Resources where oldResId='"+systemId.intValue()+"' and resourcetype='"+ConstCode.RESOURCE_TYPE_SYSTEM+"'";
				Resources res = resourcesDao.find(hsql);
				if(res==null)
				{
					//创建
					res = new Resources();
					res.setOldResId(systemId.toString());
					res.setName(system.getName());
					res.setSystem(system);
					res.setStatus(system.getStatus());
					res.setParent(null);
					res.setResourcetype(ConstCode.RESOURCE_TYPE_SYSTEM);
					res.setIsleaf(ConstCode.RESOURCE_LEAF_FALSE);
					res.setOrderid(null);
					resourcesDao.save(res);
				}
				else
				{
					res.setName(system.getName());
					resourcesDao.alter(res);
				}
				result = true;
			}
		} catch (Exception e) {
			result = false;
		}
		return result;
	}
	public boolean deleteSystem(Integer systemId)
	{
		//系统删除服务
		
		//系统删除时，先清除角色中已经分配的该系统的资源关系，然后删除资源
		//再删除系统
		boolean result = true;
		
		boolean ret = false;
		try {
			String hsql = "from Resources where oldResId='"+systemId.intValue()+"' and resourcetype='"+ConstCode.RESOURCE_TYPE_SYSTEM+"'";
			Resources res = resourcesDao.find(hsql);
			if(res!=null)
			{
				resourcesService.deleteSubResources(res.getId());
			}
		
			systemDao.deleteById(systemId);
		} catch (Exception e) {
			result = false;
		}
		return result;
	}	
}
